package com.ruoyi.web.controller.credit;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.CreditNews;
import com.ruoyi.system.service.ICreditNewsService;

/**
 * 新闻资讯Controller
 * 
 * @author ruoyi
 * @date 2025-01-28
 */
@RestController
@RequestMapping("/credit/news")
public class CreditNewsController extends BaseController
{
    @Autowired
    private ICreditNewsService creditNewsService;

    /**
     * 查询新闻资讯列表（用户端，只查询已发布的）
     */
    @PreAuthorize("@ss.hasPermi('credit:news:view')")
    @GetMapping("/list")
    public TableDataInfo list(CreditNews creditNews)
    {
        startPage();
        List<CreditNews> list = creditNewsService.selectPublishedNewsList(creditNews);
        return getDataTable(list);
    }

    /**
     * 查询新闻资讯管理列表（管理员用）
     */
    @PreAuthorize("@ss.hasPermi('credit:news:list')")
    @GetMapping("/manage/list")
    public TableDataInfo manageList(CreditNews creditNews)
    {
        startPage();
        List<CreditNews> list = creditNewsService.selectCreditNewsList(creditNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻资讯列表
     */
    @PreAuthorize("@ss.hasPermi('credit:news:export')")
    @Log(title = "新闻资讯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CreditNews creditNews)
    {
        List<CreditNews> list = creditNewsService.selectCreditNewsList(creditNews);
        ExcelUtil<CreditNews> util = new ExcelUtil<CreditNews>(CreditNews.class);
        util.exportExcel(response, list, "新闻资讯数据");
    }

    /**
     * 获取新闻资讯详细信息
     */
    @PreAuthorize("@ss.hasPermi('credit:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId)
    {
        return success(creditNewsService.selectCreditNewsByNewsId(newsId));
    }

    /**
     * 阅读新闻资讯（前端用户阅读，增加阅读量）
     */
    @PreAuthorize("@ss.hasPermi('credit:news:view')")
    @GetMapping("/read/{newsId}")
    public AjaxResult readNews(@PathVariable("newsId") Long newsId)
    {
        CreditNews news = creditNewsService.readNews(newsId);
        if (news == null) {
            return error("新闻不存在");
        }
        return success(news);
    }

    /**
     * 新增新闻资讯
     */
    @PreAuthorize("@ss.hasPermi('credit:news:add')")
    @Log(title = "新闻资讯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CreditNews creditNews)
    {
        creditNews.setCreateBy(getUsername());
        return toAjax(creditNewsService.insertCreditNews(creditNews));
    }

    /**
     * 修改新闻资讯
     */
    @PreAuthorize("@ss.hasPermi('credit:news:edit')")
    @Log(title = "新闻资讯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CreditNews creditNews)
    {
        creditNews.setUpdateBy(getUsername());
        return toAjax(creditNewsService.updateCreditNews(creditNews));
    }

    /**
     * 删除新闻资讯
     */
    @PreAuthorize("@ss.hasPermi('credit:news:remove')")
    @Log(title = "新闻资讯", businessType = BusinessType.DELETE)
	@DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds)
    {
        return toAjax(creditNewsService.deleteCreditNewsByNewsIds(newsIds));
    }

    /**
     * 发布新闻资讯
     */
    @PreAuthorize("@ss.hasPermi('credit:news:edit')")
    @Log(title = "新闻资讯", businessType = BusinessType.UPDATE)
    @PutMapping("/publish/{newsId}")
    public AjaxResult publish(@PathVariable Long newsId)
    {
        CreditNews creditNews = new CreditNews();
        creditNews.setNewsId(newsId);
        creditNews.setPublishStatus(1);
        creditNews.setPublishTime(new java.util.Date());
        creditNews.setUpdateBy(getUsername());
        return toAjax(creditNewsService.updateCreditNews(creditNews));
    }

    /**
     * 取消发布新闻资讯
     */
    @PreAuthorize("@ss.hasPermi('credit:news:edit')")
    @Log(title = "新闻资讯", businessType = BusinessType.UPDATE)
    @PutMapping("/unpublish/{newsId}")
    public AjaxResult unpublish(@PathVariable Long newsId)
    {
        CreditNews creditNews = new CreditNews();
        creditNews.setNewsId(newsId);
        creditNews.setPublishStatus(0);
        creditNews.setUpdateBy(getUsername());
        return toAjax(creditNewsService.updateCreditNews(creditNews));
    }
} 